package com.qianli.cashmama.trade.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.HttpUtil;
import com.fqgj.exception.enums.BasicErrorCodeEnum;
import com.google.common.collect.Maps;
import com.qianli.cashmama.trade.common.dto.OpenApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2020-12-02 11:49
 **/

public class CallHttpService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CallHttpService.class);

    /**
     * 调用商户http方法
     * @param url
     * @param token
     * @param bizData
     */
    public static String callMerchantMethod(String url, String token, String method, String merchantCode, String bizData){

        Map param = Maps.newHashMap();
        param.put("method", method);
        param.put("sign", "");
        param.put("merchant_id", merchantCode);
        param.put("biz_data", bizData);
        LOGGER.info("====== 请求入参 url:{}, token:{}, param:{} =====", url, token, JSONObject.toJSONString(param));
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("token", token);
        String result = HttpUtil.postPageBody(url, headers, JSONObject.toJSONString(param));
        LOGGER.info("==== 请求结果 result:{} =====", result);
        String returnResult = null;
        try {
            OpenApiResponse openApiResponse = JSONObject.parseObject(result, OpenApiResponse.class);
            if(openApiResponse.getCode() == 0){
                returnResult = JSONObject.toJSONString(openApiResponse.getData());
            }
        } catch (Exception e) {
            LOGGER.error("=== 请求异常 bizData:{}, e:{} ====", bizData, e);
        }
        return returnResult;
    }
}
